<?php
/**
 * Created by PhpStorm.
 * User: Admin
 * Date: 2020-08-01
 * Time: 下午 08:57
 */
namespace Lib;

/**
 * Class Functions
 * @package Lib
 */
class Response extends Injector {
    public function json($data) {
        header('content-type: application/json');
        echo json_encode($data);
        exit;
    }
    
    public function apiFailJson($msg='', $code='fail', $data=[]) {
        return $this->apiJson(false, $code, $msg, $data);
    }
    
    public function apiSuccessJson($msg='', $data=[], $code='ok') {
        return $this->apiJson(true, $code, $msg, $data);
    }
    
    public function apiDataJson($data, $msg='', $code='ok') {
        return $this->apiJson(true, $code, $msg, $data);
    }
    
    public function apiJson($success=true, $code='ok', $msg='', $data=[]) {
        $this->json([
            'success' => $success,
            'code' => $code,
            'msg' => $msg,
            'data' => $data,
        ]);
    }
    
    public function jsonp($data, $callback) {
        header('content-type: text/javascript');
        $json = json_encode($data);
        echo "$callback($json);";
        exit;
    }
}